import { Column, Comment, Default, HasMany, Model, Table, Unique } from 'sequelize-typescript';
import { Token } from './token.model';
import { Photo } from './photo.model';
import { DataTypes } from 'sequelize';
import md5 from 'md5';

// 阅读本章了解更多关于Sequelize的内容。
// https://github.com/sequelize/sequelize-typescript#model-association

@Table
export class User extends Model<User> {
  @Comment('用户ID，唯一')
  @Unique
  @Column({ type: DataTypes.UUID, defaultValue: DataTypes.UUIDV4, allowNull: false })
  account_id: string;

  @Comment('用户名')
  @Column({ allowNull: false })
  username: string;

  @Comment('密码(MD5)')
  @Column
  password_md5: string;

  @Column({
    type: DataTypes.VIRTUAL,
    set(val: string) {
      this.setDataValue('password_md5', md5(val).toUpperCase());
    }
  })
  password: string;

  @Comment('账号状态  1 -- 正常  2 -- 冻结')
  @Default(1)
  @Column({ allowNull: false })
  status: number;

  @Comment('昵称')
  @Column
  nickname: string;

  @Comment('手机号')
  @Column
  phone: string;

  @HasMany(() => Photo)
  photos: Photo[];

  @HasMany(() => Token)
  Tokens: Token[];
}
